#!/usr/bin/env python
# -*- coding: utf-8 -*-

import datetime
import re
import cgi
import ConfigParser

class text:
    def transfer(self,txt):

        content = txt

        # 标题
        content = re.sub(r"(?m)^=(.+?)=\s",r"<h2>\1</h2>",content)

        # 引用代码块
        content = re.sub(r"(?s):::\s+(.+?):::(?=\s+|$)",lambda m:"<quote>"+cgi.escape(m.group(1),quote=True)+"</quote>",content)

        # 高亮代码块
        content = re.sub(r"(?s)>>>(\w+?)\s+(.+?)<<<(?=\s+|$)",lambda m:"<pre class='brush: "+m.group(1)+";tab-size: 4;toolbar: false'>"+cgi.escape(m.group(2),quote=True)+"</pre>",content)

        # 简单列表
        content = re.sub(r"(?m)^\*\s(.+?)$",r"<ul><li>\1</li></ul>",content)
        content = re.sub(r"\<\/ul\>\s*\<ul\>",r"",content)

        # 有序号列表
        content = re.sub(r"(?m)^\+\s(.+?)$",r"<ol><li>\1</li></ol>",content)
        content = re.sub(r"\<\/ol\>\s*\<ol\>",r"",content)

        # 删除线
        content = re.sub(r"~~(.+?)~~",r"<s>\1</s>",content)

        # 下划线
        content = re.sub(r"__(.+?)__(?![\[\(\.])",r"<u>\1</u>",content)

        # 强调
        content = re.sub(r"@@(.+?)@@",r"<strong>\1</strong>",content)

        # 混图
        content = re.sub(r"<!--#(.+?)#-->",r"<img src='/static/upload/postfiles/\1' />",content)

        # 转换标签
        content = re.sub(r"##(.+?)##",r"<a href='#'>\1</a>",content)

        return content
 
class options:  
    def site_save(self,gacode="",default_limit=10,sitename=""):
        cf = ConfigParser.ConfigParser()
        cf.read("config.conf")
        cf.set("setting","limit",default_limit)
        cf.set("setting","sitename",sitename)
        cf.set("setting","gacode",gacode)
        cf.write(open("config.conf","w"))

    def site_options(self):
        options = {}
        cf = ConfigParser.ConfigParser()
        cf.read("config.conf")
        options["default_limit"] = cf.getint("setting","limit")
        options["sitename"] = cf.get("setting","sitename")
        options["gacode"] = cf.get("setting","gacode")
        return options
